<?php
/**
 * Created by PhpStorm.
 * User: stanley
 * Date: 2017/4/24 0024
 * Time: 9:01
 */

namespace app\Http\Controllers\Product;


use App\Http\Controllers\Controller;
use PhpParser\Node\Param;

class BrandController extends Controller
{
    const FILE_MAX_SIZE = 512 * 1024;

    function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if (!$menu_id) {
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('product.brand.list', compact('menu_id', 'toolBarButton'));
    }

    function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if (!$params['curr_menu_id']) {
            return response()->json(['code' => 400, '页面无菜单ID']);
        }
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try {
            $list = callApiServer("Product.BrandService.getInfoList", $params);
            return response()->json($list);
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 400, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }

    function showAddForm()
    {
        return view('product.brand.form');
    }

    function showEditForm()
    {
        $params['id'] = request('brand_id');
        try {
            $info = callApiServer("Product.BrandService.getInfoById", $params);
            if (empty($info)) {
                return response()->json('对应数据不存在');
            }
            return view('product.brand.form', compact("info"));
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 500, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }

    function save()
    {
        $params['id'] = request('id');
        $params['name'] = request('name');
        $params['initial'] = request('initial');
        $params['is_recommend'] = request('isRecommend', 'off') == 'on' ? 1 : 0;
        if(!empty($_FILES['pic'])){ //不为空才上传
            $file = $_FILES['pic'];
            list($result, $pic, $msg) = $this->upload($file, 'brand');
            if (!$result) {
                return response()->json(['code' => 400, 'data' => $msg]);
            }
            $params['pic'] = $pic;
        }
        if (empty($params['name'])) {
            return response()->json(['code' => 400, 'data' => '品牌名称不能为空']);
        }
        if (empty($params['initial'])) {
            return response()->json(['code' => 400, 'data' => '首字母名称不能为空']);
        }

        try {
            $rs = callApiServer("Product.BrandService.saveData", $params);
        } catch (\Exception $e) {
            return response()->json(['code' => 400, 'data' => $e->getMessage()]);
        }
        if ($rs) {
            return response()->json(['code' => 200]);
        }
        return response()->json(['code' => 400, 'data' => $rs]);
    }

    private function upload($file, $type)
    {
        if ($file['error'] != 0) return [false, '', '上传失败'];
        if ($file['size'] > self::FILE_MAX_SIZE) return [false, '', '文件尺寸超过500k'];
        if (!getimagesize($file['tmp_name'])) return [false, '', '请上传正确格式的图片'];
        $name = md5($file['tmp_name'] . time());
        $ext = explode('/', $file['type'])[1];
        $dest = env('RESOURCE_PATH') . '/' . $type . '/' . date('ymd');
        if (!is_dir($dest)) {
            mkdir($dest, 0777, true);
            chmod($dest, 0777);
        }
        $r = move_uploaded_file($file['tmp_name'], $dest . '/' . $name . '.' . $ext);
        if ($r) {
            return [true, '/' . $type . '/' . date('ymd') . '/' . $name . '.' . $ext, 'success'];
        } else {
            return [false, '', '上传失败'];
        }
    }

    function getList(){
        try{
            $list = callApiServer('Product.BrandService.getInfoList');
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }
}